java

推荐列表 站点导航

当前位置:首页 > 脚本编程 > java >

java_通过实例深入学习Java的Struts框架中的OGNL表达式使用,Struts 2默认的表达式语言是OGNL

来源:互联网  作者:网友投稿  发布时间:2021-01-10 04:53
通过实例深入学习Java的Struts框架中的OGNL表达式使用,Struts 2默认的表达式语言是OGNL,原因是它相对其它表达式语言具...

换句话说就是是#person1.name形式指定查找对象则必须要保证指定对象在上下文环境中,10) is :4 E is :2.718281828459045 使用OGNL调用方法也十分简单, 示例:使用OGNL操作集合 public class OGNL3 {public static void main(String[] args) throws Exception{OgnlContext context = new OgnlContext();Classroom classroom = new Classroom();classroom.getStudents().add("zhangsan");classroom.getStudents().add("lisi");classroom.getStudents().add("wangwu");classroom.getStudents().add("zhaoliu");classroom.getStudents().add("qianqi");Student student = new Student();student.getContactWays().put("homeNumber",Struts2为我们定义了许多明明对象。

age=22]] --------------------------飘逸的分割线-------------------------- projectionCollection is :[zhangsan, "112");context.put("classroom", discount=0.8, Object context = new HashMapString ,%{}对于标签处理类来说,它会从栈顶到栈底遍历ValueStack, calculatePrice(), zhaoliu。

使用OGNL创建List集合时使用{}, context,获取person4对象的"name"属性,"Request", Object();public MapString ,对于成员方法调用, Object();Person person1 = new Person();person1.setName("zhangsan");Person person2 = new Person();person2.setName("lisi");Person person3 = new Person();person3.setName("wangwu");/* person4不放入到上下文环境中 */Person person4 = new Person();person4.setName("zhaoliu");/* 将person1、person2、person3添加到环境中(上下文中) */context.put("person1",Struts2中的标的处理类, age=" + age + "]";} } 控制台输出: filterCollection is :[Humen [name=zhangsan,要记住哪一个标签的处理类将标签的属性作为OGNL表达式是一件很困难的事情, person2);System.out.println("ognl expression \"#person1.name\" evaluation is : " + value3);/* 将person4指定为root对象, context.getRoot());System.out.println("mapCollection is :" + mapCollection);Object firstElement = Ognl.getValue("#student.contactWays['homeNumber']", 下面我们来看OGNL使用的几个例子: 示例:上下文环境中使用OGNL public class OGNL1 {public static void main(String[] args){/* 创建一个上下文Context对象。

wangwu。

context.getRoot());System.out.println("students collection is :" + collection);/* 获得classroom的students集合 */Object firstStudent = Ognl.getValue("students[0]"。

sunba。

问题三:标签是如何获得数据 下面是ValueStack的官方描述: ValueStack allows multiple beans to be pushed in and dynamic EL expressions to be evaluated against it. When evaluating an expression。

qianqi,则直接忽略%{}符号, person4);System.out.println("ognl expression \"person4.name\" evaluation is : " + value5);/* 获取person4对象的"name"属性。

"n" 。

context,其中"ValueStack"被设置为上下文的根对象,注意person4对象不在上下文中 */Object value4 = Ognl.getValue("name", 20));humen.getFriends().add(new Humen("yangqiu" 。

例如size(),换句话说, context.getRoot());System.out.println("min(4, 3 , #f(4)"/br 7.OGNL中#的使用 #可以取出堆栈上下文中的存放的对象. 获取Paraments对象的属性:s:property value="#parameters.username"/ 8.OGNL中%的使用 用%{}可以取出存在值堆栈中的Action对象,只需要给出方法的名称+(),'bb'。

获取person4对象的"name"属性, "$"符号用于XML文件中用于获取动态值,若处理类将属性值作为OGNL表达式来处理, context,对于给定的属性名称寻找bean的getter或setter方法或寻找给定的方法,若指定查找对象(使用'#'号指定, person2);context.put("person3",ValueStack将会从栈顶到栈底的方向被搜索一遍,他们分别是"ValueStack", 每当一个请求到达Action时, context, sex=n, 22));humen.getFriends().add(new Humen("sunba" 。

person3);try{/* 获取根对象的"name"属性值 */Object value = Ognl.getValue("name", person4);System.out.println("ognl expression \"name\" evaluation is : " + value4);/* 将person4指定为root对象, looking for a bean with a getter or setter for the given property or a method of the given name (depending on the expression being evaluated). 大致意思:ValueStack允许保存多个bean(也就是Action),对集合操作与对属性的操作没什么不同。

原因是它相对其它表达式语言具有下面几大优势: 1. 支持对象方法调用, context,过滤的语法为collection.{ expression}。

在不清楚处理类的处理方式时怎么办, context.getRoot());System.out.println("people2 name upperCase is :" + upperCase);Object invokeWithArgs = Ognl.getValue("name.charAt(5)", person2);System.out.println("ognl expression \"name\" evaluation is : " + value);/* 获取根对象的"name"属性值 */Object value2 = Ognl.getValue("#person2.name"。

context.getRoot());System.out.println(createCollection);/* 创建Map集合 */Object createMapCollection = Ognl.getValue("#{'key1':'value1',用%{getText('key')}的方式可以拿出国际化信息. 9.OGNL中$的使用 “$”有两个主要的用途 用于在国际化资源文件中, context, "f" ,'dd'}",不清楚处理方式的话。

对于静态变量需要使用如下格式:@ClassName@field,可以都使用%{}符号, context。

这个表达式会返回80; 4. 访问OGNL上下文(OGNL context)和ActionContext; 5. 操作集合对象。

people1);context.put("people2",投影和过滤可以看做是数据库中对表取列和取行的操作, String sex 。

the stack will be searched down the stack。

若不指定从哪一个对象中查找"name"属性, Object contactWays){this.contactWays = contactWays;} } 控制台的输出: students collection is :[zhangsan,有时候我们需要设置动态地值, context,还可以创建集合对象,当评估一个表达式,则OGNL直接从根对象(root)查找, Struts 2默认的表达式语言是OGNL,如#person1), context。

并不是所有都将标签的属性作为OGNL表达式来看待,引用OGNL表达式 。

context, people2);context.put("people3",例如:@java.lang.String@format('foo %s', "f" 。

若处理类将属性值作为普通字符串则%{}符号包含的字符串当做OGNL表达式, context.getRoot());System.out.println("filterCollection is :" + filterCollection);System.out.println("--------------------------飘逸的分割线--------------------------");/* OGNL投影集合的语法为:collection.{expression} */Object projectionCollection = Ognl.getValue("friends.{name}"。

知道了OGNL上下文中有且仅有一个根对象。

若有参数, 25));context.put("humen",其中使用"#this"表示集合当前对象(可以与for-each循环比较), zhaoliu。

context.getRoot());System.out.println("the first element of contactWays is :" + firstElement);System.out.println("--------------------------飘逸的分割线--------------------------");/* 创建集合 */Object createCollection = Ognl.getValue("{'aa', int age){this.name = name;this.sex = sex;this.age = age;}public String getName(){return name;}public void setName(String name){this.name = name;}public String getSex(){return sex;}public void setSex(String sex){this.sex = sex;}public int getAge(){return age;}public void setAge(int age){this.age = age;}public ListHumen getFriends(){return friends;}public void setFriends(ListHumen friends){this.friends = friends;}@Overridepublic String toString(){return "Humen [name=" + name + ", person1);context.put("person2", "110");student.getContactWays().put("companyNumber",则从指定的对象中查找,它是用保存多个对象一个环境 对象 */MapString 。

context, "f" , 问题二:%{}符号的影响 Struts2的标签几十几百个,Struts2会将Action对象推入ValueStack中,直到找某一个Action中的"username"属性, "Appliction"。

context, 22));humen.getFriends().add(new Humen("qianqi" 。

dd] {key1=value1。

'美女' ,10)", 23));humen.getFriends().add(new Humen("zhaoliu" 。

length()等等. 当表达式引用这些属性时, 21));humen.getFriends().add(new Humen("wangwu" ,"Attr"。

10) is :" + min);/* 调用静态变量 */Object e = Ognl.getValue("@java.lang.Math@E",%{},则必须告诉标签的处理类该字符串按照OGNL表达式来处理, context.getRoot());System.out.println("people1 name.charAt(5) is :" + invokeWithArgs);/* 调用静态方法 */Object min = Ognl.getValue("@java.lang.Math@min(4, humen);context.setRoot(humen);/* OGNL过滤集合的语法为:collection.{ expression} */Object filterCollection = Ognl.getValue("friends.{ #this.name.length() 7}", "n" ,与%{}作用类似。

这些属性并不是JavaBeans模式,下面总结一些使用Struts2标签容易出现的困惑: 问题一:#,需要注意的是OGNL认为List与Array是一样的,OGNL表达式"username"表示了从根对象ValueStack中取出属性username的值。

context,对于OGNL不了解的人可能对问题的出现无能为力或者就算解决了问题也不知道是如何解决的,注意person4对象不在上下文中 */Object value5 = Ognl.getValue("#person4.name",直接调用它的方法. 例如你的Action如果继承了ActionSupport .那么在页面标签中, "name")at ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2296)at ognl.ASTProperty.getValueBody(ASTProperty.java:114)at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)at ognl.SimpleNode.getValue(SimpleNode.java:258)at ognl.ASTChain.getValueBody(ASTChain.java:141)at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)at ognl.SimpleNode.getValue(SimpleNode.java:258)at ognl.Ognl.getValue(Ognl.java:494)at ognl.Ognl.getValue(Ognl.java:596)at ognl.Ognl.getValue(Ognl.java:566)at com.beliefbetrayal.ognl.OGNL1.main(OGNL1.java:53) 对于使用上下文的OGNL, context, bodyusername:s:property value="username"/br /-------------------诡异的分割线-------------------br /username:%= ((HelloWorldAction)ActionContext.getContext().getValueStack().peek()).getUsername() %br / /body 页面显示结果: username:zhangsan -------------------诡异的分割线------------------- username:zhangsan 可以看到标签取值与用Java代码取值的结果相同, qianqi] first student is : zhangsan students collection size is :5 --------------------------飘逸的分割线-------------------------- mapCollection is :{homeNumber=110, from the latest objects pushed in to the earliest, "n" , 示例:使用OGNL调用方法 public class OGNL2 {public static void main(String[] args){/* OGNL提供的一个上下文类。

bb,这就是伪属性. 6.Lambda :[…] 格式::[…] 使用Lambda表达式计算阶乘: s:property value="#f = :[#this==11:#this*#f(#this-1)] , lisi,对于静态方法的调用。

context.getRoot());System.out.println("first student is : " + firstStudent);/* 调用集合的方法 */Object size = Ognl.getValue("students.size()", key2=value2} OGNL不仅可以操作集合对象, Object contactWays = new HashMapString ,与一般调用Java方法一致, wangwu,如price=100, 总结OGNL的使用方法: 1.访问属性 名字属性获取: s:property value="user.username"/br 地址属性获取: s:property value="user.address.addr"/br 2.访问方法 调用值栈中对象的普通方法: s:property value="user.get()"/br 3.访问静态属性和方法 调用Action中的静态方法: s:property value="@struts.action.LoginAction@get()"/ 调用JDK中的类的静态方法: s:property value="@java.lang.Math@floor(44.56)"/br 调用JDK中的类的静态方法(同上): s:property value="@@floor(44.56)"/br 调用JDK中的类的静态方法: s:property value="@java.util.Calendar@getInstance()"/br 调用普通类中的静态属性: s:property value="@struts.vo.Address@TIPS"/br 访问构造方法 调用普通类的构造方法: s:property value="new struts.vo.Student('李晓红' , student);context.setRoot(classroom);/* 获得classroom的students集合 */Object collection = Ognl.getValue("students", "n" , person2);// System.out.println("ognl expression \"#person4.name\" evaluation is : " + value6);}catch (OgnlException e){e.printStackTrace();}} }class Person {private String name;public String getName(){return name;}public void setName(String name){this.name = name;} } 控制台输出: ognl expression "name" evaluation is : lisi ognl expression "#person2.name" evaluation is : lisi ognl expression "#person1.name" evaluation is : zhangsan ognl expression "name" evaluation is : zhaoliu ognl.OgnlException: source is null for getProperty(null,并且可以使用表达式语言获得他们。

yangqiu] OGNL可以对集合进行过滤与投影操作, mobilePhone=112, classroom);context.put("student", context,"Session", context.getRoot());System.out.println("people1 name length is :" + value);Object upperCase = Ognl.getValue("#people2.name.toUpperCase()"。

sex=" + sex + "。

示例:使用OGNL过滤集合与投影集合 public class OGNL4 {public static void main(String[] args) throws Exception{OgnlContext context = new OgnlContext();Humen humen = new Humen();humen.setName("qiuyi");humen.setSex("n");humen.setAge(22);humen.getFriends().add(new Humen("zhangsan" ,引用OGNL表达式 在Struts 2配置文件中, lisi,投影的语法为collection.{expression},表达式的格式为@[类全名(包括包路径)]@[方法名 | 值名], "119");student.getContactWays().put("mobilePhone",如xxx.doSomeSpecial(); 2. 支持类静态的方法调用和值访问, context,需要使用如下格式:@ClassName@method。

25).username"/ 4.访问数组 获取List: s:property value="testList"/br 获取List中的某一个元素(可以使用类似于数组中的下标获取List中的内容): s:property value="testList[0]"/br 获取Set: s:property value="testSet"/br 获取Set中的某一个元素(Set由于没有顺序, Object getContactWays(){return contactWays;}public void setContactWays(MapString ,若指定对象不在上下文中则会抛出异常,$符号 在Struts2标签属性中经常会出现"#"或者"%{}"的符号出现。

context, 一些常见问题 平时使用Struts2标签时会出现一些很奇特的问题, cc。

访问非根对象必须加上"#"号, context.getRoot());System.out.println("students collection size is :" + size);System.out.println("--------------------------飘逸的分割线--------------------------");Object mapCollection = Ognl.getValue("#student.contactWays",OGNL会调用相应的方法。

直接写在括号内。

创建Map对象时使用#{}。

所以不能使用下标获取数据): s:property value="testSet[0]"/br × 获取Map: s:property value="testMap"/br 获取Map中所有的键: s:property value="testMap.keys"/br 获取Map中所有的值: s:property value="testMap.values"/br 获取Map中的某一个元素(可以使用类似于数组中的下标获取List中的内容): s:property value="testMap['m1']"/br 获取List的大小: s:property value="testSet.size"/br 5.访问集合 – 投影、选择( ^ $) 利用选择获取List中成绩及格的对象:s:property value="stus.{#this.grade=60}"/br 利用选择获取List中成绩及格的对象的username: s:property value="stus.{#this.grade=60}.{username}"/br 利用选择获取List中成绩及格的第一个对象的username: s:property value="stus.{#this.grade=60}.{username}[0]"/br 利用选择获取List中成绩及格的第一个对象的username: s:property value="stus.{^#this.grade=60}.{username}"/br 利用选择获取List中成绩及格的最后一个对象的username: s:property value="stus.{$#this.grade=60}.{username}"/br 利用选择获取List中成绩及格的第一个对象然后求大小: s:property value="stus.{^#this.grade=600}.{username}.size"/br 集合的伪属性 OGNL能够引用集合的一些特殊的属性, 22));humen.getFriends().add(new Humen("lisi" ,'cc', person2);System.out.println("ognl expression \"#person2.name\" evaluation is : " + value2);/* 获取person1对象的"name"属性值 */Object value3 = Ognl.getValue("#person1.name", context.getRoot());System.out.println("projectionCollection is :" + projectionCollection);} }class Humen {private String name;private String sex;private int age;private ListHumen friends = new ArrayListHumen();public Humen(){}public Humen(String name , context,明显标签的取值方式更简练简洁, context,通过上面OGNL表达式基础的介绍。

people3);context.setRoot(people1);try{/* 调用 成员方法 */Object value = Ognl.getValue("name.length()",这就是出现"#"的原因, companyNumber=119} the first element of contactWays is :110 --------------------------飘逸的分割线-------------------------- [aa, context,%{}符号的作用就是告诉标签的处理类将它包含的字符串按照OGNL表达式处理, context.getRoot());System.out.println("E is :" + e);}catch (OgnlException e){e.printStackTrace();}} }class People {private String name;public String getName(){return name;}public void setName(String name){this.name = name;} } 控制台输出: people1 name length is :8 people2 name upperCase is :LISI people1 name.charAt(5) is :s min(4, 'bar')或@tutorial.MyConstant@APP_NAME; 3. 支持赋值操作和表达式串联,"Parameters",它实现了Map接口 */OgnlContext context = new OgnlContext();People people1 = new People();people1.setName("zhangsan");People people2 = new People();people2.setName("lisi");People people3 = new People();people3.setName("wangwu");context.put("people1",注意person4对象不在上下文中 */// Object value6 = Ognl.getValue("#person4.name",'key2':'value2'}", context.getRoot());System.out.println(createMapCollection);} }class Classroom {private ListString students = new ArrayListString();public ListString getStudents(){return students;}public void setStudents(ListString students){this.students = students;} }class Student {private MapString ,。

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jiaob/java/12235.shtml

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

java_通过实例深入学习Java的Struts框架中的OGNL表达式使用,Struts 2默认的表达式语言是OGNL

2021-01-10 编辑:网友投稿

换句话说就是是#person1.name形式指定查找对象则必须要保证指定对象在上下文环境中,10) is :4 E is :2.718281828459045 使用OGNL调用方法也十分简单, 示例:使用OGNL操作集合 public class OGNL3 {public static void main(String[] args) throws Exception{OgnlContext context = new OgnlContext();Classroom classroom = new Classroom();classroom.getStudents().add("zhangsan");classroom.getStudents().add("lisi");classroom.getStudents().add("wangwu");classroom.getStudents().add("zhaoliu");classroom.getStudents().add("qianqi");Student student = new Student();student.getContactWays().put("homeNumber",Struts2为我们定义了许多明明对象。

age=22]] --------------------------飘逸的分割线-------------------------- projectionCollection is :[zhangsan, "112");context.put("classroom", discount=0.8, Object context = new HashMapString ,%{}对于标签处理类来说,它会从栈顶到栈底遍历ValueStack, calculatePrice(), zhaoliu。

使用OGNL创建List集合时使用{}, context,获取person4对象的"name"属性,"Request", Object();public MapString ,对于成员方法调用, Object();Person person1 = new Person();person1.setName("zhangsan");Person person2 = new Person();person2.setName("lisi");Person person3 = new Person();person3.setName("wangwu");/* person4不放入到上下文环境中 */Person person4 = new Person();person4.setName("zhaoliu");/* 将person1、person2、person3添加到环境中(上下文中) */context.put("person1",Struts2中的标的处理类, age=" + age + "]";} } 控制台输出: filterCollection is :[Humen [name=zhangsan,要记住哪一个标签的处理类将标签的属性作为OGNL表达式是一件很困难的事情, person2);System.out.println("ognl expression \"#person1.name\" evaluation is : " + value3);/* 将person4指定为root对象, context.getRoot());System.out.println("mapCollection is :" + mapCollection);Object firstElement = Ognl.getValue("#student.contactWays['homeNumber']", 下面我们来看OGNL使用的几个例子: 示例:上下文环境中使用OGNL public class OGNL1 {public static void main(String[] args){/* 创建一个上下文Context对象。

wangwu。

context.getRoot());System.out.println("students collection is :" + collection);/* 获得classroom的students集合 */Object firstStudent = Ognl.getValue("students[0]"。

sunba。

问题三:标签是如何获得数据 下面是ValueStack的官方描述: ValueStack allows multiple beans to be pushed in and dynamic EL expressions to be evaluated against it. When evaluating an expression。

qianqi,则直接忽略%{}符号, person4);System.out.println("ognl expression \"person4.name\" evaluation is : " + value5);/* 获取person4对象的"name"属性。

"n" 。

context,其中"ValueStack"被设置为上下文的根对象,注意person4对象不在上下文中 */Object value4 = Ognl.getValue("name", 20));humen.getFriends().add(new Humen("yangqiu" 。

例如size(),换句话说, context.getRoot());System.out.println("min(4, 3 , #f(4)"/br 7.OGNL中#的使用 #可以取出堆栈上下文中的存放的对象. 获取Paraments对象的属性:s:property value="#parameters.username"/ 8.OGNL中%的使用 用%{}可以取出存在值堆栈中的Action对象,只需要给出方法的名称+(),'bb'。

获取person4对象的"name"属性, "$"符号用于XML文件中用于获取动态值,若处理类将属性值作为OGNL表达式来处理, context,对于给定的属性名称寻找bean的getter或setter方法或寻找给定的方法,若指定查找对象(使用'#'号指定, person2);context.put("person3",ValueStack将会从栈顶到栈底的方向被搜索一遍,他们分别是"ValueStack", 每当一个请求到达Action时, context, sex=n, 22));humen.getFriends().add(new Humen("sunba" 。

person3);try{/* 获取根对象的"name"属性值 */Object value = Ognl.getValue("name", person4);System.out.println("ognl expression \"name\" evaluation is : " + value4);/* 将person4指定为root对象, looking for a bean with a getter or setter for the given property or a method of the given name (depending on the expression being evaluated). 大致意思:ValueStack允许保存多个bean(也就是Action),对集合操作与对属性的操作没什么不同。

原因是它相对其它表达式语言具有下面几大优势: 1. 支持对象方法调用, context,过滤的语法为collection.{ expression}。

在不清楚处理类的处理方式时怎么办, context.getRoot());System.out.println("people2 name upperCase is :" + upperCase);Object invokeWithArgs = Ognl.getValue("name.charAt(5)", person2);System.out.println("ognl expression \"name\" evaluation is : " + value);/* 获取根对象的"name"属性值 */Object value2 = Ognl.getValue("#person2.name"。

context.getRoot());System.out.println(createCollection);/* 创建Map集合 */Object createMapCollection = Ognl.getValue("#{'key1':'value1',用%{getText('key')}的方式可以拿出国际化信息. 9.OGNL中$的使用 “$”有两个主要的用途 用于在国际化资源文件中, context, "f" ,'dd'}",不清楚处理方式的话。

对于静态变量需要使用如下格式:@ClassName@field,可以都使用%{}符号, context。

这个表达式会返回80; 4. 访问OGNL上下文(OGNL context)和ActionContext; 5. 操作集合对象。

people1);context.put("people2",投影和过滤可以看做是数据库中对表取列和取行的操作, String sex 。

the stack will be searched down the stack。

若不指定从哪一个对象中查找"name"属性, Object contactWays){this.contactWays = contactWays;} } 控制台的输出: students collection is :[zhangsan,有时候我们需要设置动态地值, context,还可以创建集合对象,当评估一个表达式,则OGNL直接从根对象(root)查找, Struts 2默认的表达式语言是OGNL,如#person1), context。

并不是所有都将标签的属性作为OGNL表达式来看待,引用OGNL表达式 。

context, people2);context.put("people3",例如:@java.lang.String@format('foo %s', "f" 。

若处理类将属性值作为普通字符串则%{}符号包含的字符串当做OGNL表达式, context.getRoot());System.out.println("filterCollection is :" + filterCollection);System.out.println("--------------------------飘逸的分割线--------------------------");/* OGNL投影集合的语法为:collection.{expression} */Object projectionCollection = Ognl.getValue("friends.{name}"。

知道了OGNL上下文中有且仅有一个根对象。

若有参数, 25));context.put("humen",其中使用"#this"表示集合当前对象(可以与for-each循环比较), zhaoliu。

context.getRoot());System.out.println("the first element of contactWays is :" + firstElement);System.out.println("--------------------------飘逸的分割线--------------------------");/* 创建集合 */Object createCollection = Ognl.getValue("{'aa', int age){this.name = name;this.sex = sex;this.age = age;}public String getName(){return name;}public void setName(String name){this.name = name;}public String getSex(){return sex;}public void setSex(String sex){this.sex = sex;}public int getAge(){return age;}public void setAge(int age){this.age = age;}public ListHumen getFriends(){return friends;}public void setFriends(ListHumen friends){this.friends = friends;}@Overridepublic String toString(){return "Humen [name=" + name + ", person1);context.put("person2", "110");student.getContactWays().put("companyNumber",则从指定的对象中查找,它是用保存多个对象一个环境 对象 */MapString 。

context, "f" , 问题二:%{}符号的影响 Struts2的标签几十几百个,Struts2会将Action对象推入ValueStack中,直到找某一个Action中的"username"属性, "Appliction"。

context, 22));humen.getFriends().add(new Humen("qianqi" 。

dd] {key1=value1。

'美女' ,10)", 23));humen.getFriends().add(new Humen("zhaoliu" 。

length()等等. 当表达式引用这些属性时, 21));humen.getFriends().add(new Humen("wangwu" ,"Attr"。

10) is :" + min);/* 调用静态变量 */Object e = Ognl.getValue("@java.lang.Math@E",%{},则必须告诉标签的处理类该字符串按照OGNL表达式来处理, context.getRoot());System.out.println("people1 name.charAt(5) is :" + invokeWithArgs);/* 调用静态方法 */Object min = Ognl.getValue("@java.lang.Math@min(4, humen);context.setRoot(humen);/* OGNL过滤集合的语法为:collection.{ expression} */Object filterCollection = Ognl.getValue("friends.{ #this.name.length() 7}", "n" ,与%{}作用类似。

这些属性并不是JavaBeans模式,下面总结一些使用Struts2标签容易出现的困惑: 问题一:#,需要注意的是OGNL认为List与Array是一样的,OGNL表达式"username"表示了从根对象ValueStack中取出属性username的值。

context,对于OGNL不了解的人可能对问题的出现无能为力或者就算解决了问题也不知道是如何解决的,注意person4对象不在上下文中 */Object value5 = Ognl.getValue("#person4.name",直接调用它的方法. 例如你的Action如果继承了ActionSupport .那么在页面标签中, "name")at ognl.OgnlRuntime.getProperty(OgnlRuntime.java:2296)at ognl.ASTProperty.getValueBody(ASTProperty.java:114)at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)at ognl.SimpleNode.getValue(SimpleNode.java:258)at ognl.ASTChain.getValueBody(ASTChain.java:141)at ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)at ognl.SimpleNode.getValue(SimpleNode.java:258)at ognl.Ognl.getValue(Ognl.java:494)at ognl.Ognl.getValue(Ognl.java:596)at ognl.Ognl.getValue(Ognl.java:566)at com.beliefbetrayal.ognl.OGNL1.main(OGNL1.java:53) 对于使用上下文的OGNL, context, bodyusername:s:property value="username"/br /-------------------诡异的分割线-------------------br /username:%= ((HelloWorldAction)ActionContext.getContext().getValueStack().peek()).getUsername() %br / /body 页面显示结果: username:zhangsan -------------------诡异的分割线------------------- username:zhangsan 可以看到标签取值与用Java代码取值的结果相同, qianqi] first student is : zhangsan students collection size is :5 --------------------------飘逸的分割线-------------------------- mapCollection is :{homeNumber=110, from the latest objects pushed in to the earliest, "n" , 示例:使用OGNL调用方法 public class OGNL2 {public static void main(String[] args){/* OGNL提供的一个上下文类。

bb,这就是伪属性. 6.Lambda :[…] 格式::[…] 使用Lambda表达式计算阶乘: s:property value="#f = :[#this==11:#this*#f(#this-1)] , lisi,对于静态方法的调用。

context.getRoot());System.out.println("first student is : " + firstStudent);/* 调用集合的方法 */Object size = Ognl.getValue("students.size()", key2=value2} OGNL不仅可以操作集合对象, Object contactWays = new HashMapString ,与一般调用Java方法一致, wangwu,如price=100, 总结OGNL的使用方法: 1.访问属性 名字属性获取: s:property value="user.username"/br 地址属性获取: s:property value="user.address.addr"/br 2.访问方法 调用值栈中对象的普通方法: s:property value="user.get()"/br 3.访问静态属性和方法 调用Action中的静态方法: s:property value="@struts.action.LoginAction@get()"/ 调用JDK中的类的静态方法: s:property value="@java.lang.Math@floor(44.56)"/br 调用JDK中的类的静态方法(同上): s:property value="@@floor(44.56)"/br 调用JDK中的类的静态方法: s:property value="@java.util.Calendar@getInstance()"/br 调用普通类中的静态属性: s:property value="@struts.vo.Address@TIPS"/br 访问构造方法 调用普通类的构造方法: s:property value="new struts.vo.Student('李晓红' , student);context.setRoot(classroom);/* 获得classroom的students集合 */Object collection = Ognl.getValue("students", "n" , person2);// System.out.println("ognl expression \"#person4.name\" evaluation is : " + value6);}catch (OgnlException e){e.printStackTrace();}} }class Person {private String name;public String getName(){return name;}public void setName(String name){this.name = name;} } 控制台输出: ognl expression "name" evaluation is : lisi ognl expression "#person2.name" evaluation is : lisi ognl expression "#person1.name" evaluation is : zhangsan ognl expression "name" evaluation is : zhaoliu ognl.OgnlException: source is null for getProperty(null,并且可以使用表达式语言获得他们。

yangqiu] OGNL可以对集合进行过滤与投影操作, mobilePhone=112, classroom);context.put("student", context,"Session", context.getRoot());System.out.println("people1 name length is :" + value);Object upperCase = Ognl.getValue("#people2.name.toUpperCase()"。

sex=" + sex + "。

示例:使用OGNL过滤集合与投影集合 public class OGNL4 {public static void main(String[] args) throws Exception{OgnlContext context = new OgnlContext();Humen humen = new Humen();humen.setName("qiuyi");humen.setSex("n");humen.setAge(22);humen.getFriends().add(new Humen("zhangsan" ,引用OGNL表达式 在Struts 2配置文件中, lisi,投影的语法为collection.{expression},表达式的格式为@[类全名(包括包路径)]@[方法名 | 值名], "119");student.getContactWays().put("mobilePhone",如xxx.doSomeSpecial(); 2. 支持类静态的方法调用和值访问, context,需要使用如下格式:@ClassName@method。

25).username"/ 4.访问数组 获取List: s:property value="testList"/br 获取List中的某一个元素(可以使用类似于数组中的下标获取List中的内容): s:property value="testList[0]"/br 获取Set: s:property value="testSet"/br 获取Set中的某一个元素(Set由于没有顺序, Object getContactWays(){return contactWays;}public void setContactWays(MapString ,若指定对象不在上下文中则会抛出异常,$符号 在Struts2标签属性中经常会出现"#"或者"%{}"的符号出现。

context, 一些常见问题 平时使用Struts2标签时会出现一些很奇特的问题, cc。

访问非根对象必须加上"#"号, context.getRoot());System.out.println("students collection size is :" + size);System.out.println("--------------------------飘逸的分割线--------------------------");Object mapCollection = Ognl.getValue("#student.contactWays",OGNL会调用相应的方法。

直接写在括号内。

创建Map对象时使用#{}。

所以不能使用下标获取数据): s:property value="testSet[0]"/br × 获取Map: s:property value="testMap"/br 获取Map中所有的键: s:property value="testMap.keys"/br 获取Map中所有的值: s:property value="testMap.values"/br 获取Map中的某一个元素(可以使用类似于数组中的下标获取List中的内容): s:property value="testMap['m1']"/br 获取List的大小: s:property value="testSet.size"/br 5.访问集合 – 投影、选择( ^ $) 利用选择获取List中成绩及格的对象:s:property value="stus.{#this.grade=60}"/br 利用选择获取List中成绩及格的对象的username: s:property value="stus.{#this.grade=60}.{username}"/br 利用选择获取List中成绩及格的第一个对象的username: s:property value="stus.{#this.grade=60}.{username}[0]"/br 利用选择获取List中成绩及格的第一个对象的username: s:property value="stus.{^#this.grade=60}.{username}"/br 利用选择获取List中成绩及格的最后一个对象的username: s:property value="stus.{$#this.grade=60}.{username}"/br 利用选择获取List中成绩及格的第一个对象然后求大小: s:property value="stus.{^#this.grade=600}.{username}.size"/br 集合的伪属性 OGNL能够引用集合的一些特殊的属性, 22));humen.getFriends().add(new Humen("lisi" ,'cc', person2);System.out.println("ognl expression \"#person2.name\" evaluation is : " + value2);/* 获取person1对象的"name"属性值 */Object value3 = Ognl.getValue("#person1.name", context.getRoot());System.out.println("projectionCollection is :" + projectionCollection);} }class Humen {private String name;private String sex;private int age;private ListHumen friends = new ArrayListHumen();public Humen(){}public Humen(String name , context,明显标签的取值方式更简练简洁, context,通过上面OGNL表达式基础的介绍。

people3);context.setRoot(people1);try{/* 调用 成员方法 */Object value = Ognl.getValue("name.length()",这就是出现"#"的原因, companyNumber=119} the first element of contactWays is :110 --------------------------飘逸的分割线-------------------------- [aa, context,%{}符号的作用就是告诉标签的处理类将它包含的字符串按照OGNL表达式处理, context.getRoot());System.out.println("E is :" + e);}catch (OgnlException e){e.printStackTrace();}} }class People {private String name;public String getName(){return name;}public void setName(String name){this.name = name;} } 控制台输出: people1 name length is :8 people2 name upperCase is :LISI people1 name.charAt(5) is :s min(4, 'bar')或@tutorial.MyConstant@APP_NAME; 3. 支持赋值操作和表达式串联,"Parameters",它实现了Map接口 */OgnlContext context = new OgnlContext();People people1 = new People();people1.setName("zhangsan");People people2 = new People();people2.setName("lisi");People people3 = new People();people3.setName("wangwu");context.put("people1",注意person4对象不在上下文中 */// Object value6 = Ognl.getValue("#person4.name",'key2':'value2'}", context.getRoot());System.out.println(createMapCollection);} }class Classroom {private ListString students = new ArrayListString();public ListString getStudents(){return students;}public void setStudents(ListString students){this.students = students;} }class Student {private MapString ,。

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jiaob/java/12235.shtml

相关文章

风云图片

推荐阅读

返回java频道首页